import thread
import time
import pygame as pg
from pygame.locals import *
from costants import *
##########################################################
##########################################################
pg.font.init()
try: font=pygame.font.Font(FONT,FONT_DIM)
except IOError: font=pygame.font.Font(ALTERNATIVE_FONT,FONT_DIM)
##########################################################
##########################################################
class Timer:
	def __init__(self, start_time):
		self.__lock = thread.allocate_lock()
		self.__interval = 1.0
		self.__start_time=start_time
		self.remaining_time=start_time
		self.__loop = False
		self.__alive = False
		
	def start(self):
		self.__lock.acquire()
		if not self.__alive:
			self.__loop = True
			self.__alive = True
			thread.start_new_thread(self.__run, ())
		self.__lock.release()
		
	def stop(self):
		self.__lock.acquire()
		self.__loop = False
		self.__lock.release()
			
	def __run(self):
		while self.__loop:
			time.sleep(self.__interval)
			self.remaining_time -= 1
		self.__alive = False
		
	def blit(self,screen):
		time=font.render("Time: " + str(self.remaining_time), True, TIME_COLOR)
		screen.blit(time,time.get_rect(center=ARK_BALLOON_CENTER))
##########################################################
##########################################################
